<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解 synchronized：Java 中的悲观锁机制</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.05'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
        }
        
        .content-card {
            background: white;
            border-radius: 16px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 12px 20px;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        
        .code-dots {
            display: flex;
            gap: 8px;
        }
        
        .code-dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
            position: relative;
            overflow: hidden;
        }
        
        .highlight-box::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: pulse 3s ease-in-out infinite;
        }
        
        @keyframes pulse {
            0%, 100% { transform: scale(1); opacity: 0.5; }
            50% { transform: scale(1.1); opacity: 0.3; }
        }
        
        .feature-card {
            background: white;
            border-radius: 12px;
            padding: 24px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
            border: 2px solid transparent;
        }
        
        .feature-card:hover {
            border-color: #667eea;
            transform: translateY(-5px);
            box-shadow: 0 15px 40px rgba(102, 126, 234, 0.2);
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 40px 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 8px;
            margin-top: -8px;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient relative">
        <div class="container mx-auto px-6 py-20 relative z-10">
            <div class="text-center text-white">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 tracking-tight">
                    深入理解 synchronized
                </h1>
                <p class="text-xl md:text-2xl opacity-90 mb-8">
                    Java 中的悲观锁机制与并发控制艺术
                </p>
                <div class="flex justify-center gap-4 text-sm">
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                        <i class="fas fa-lock mr-2"></i>并发编程
                    </span>
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                        <i class="fas fa-code mr-2"></i>Java 核心
                    </span>
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                        <i class="fas fa-shield-alt mr-2"></i>线程安全
                    </span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="content-card p-8 mb-12 -mt-16 relative z-20">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">s</span>ynchronized 关键字被称为"悲观锁"主要是因为它的工作原理和设计思路基于一种"悲观"假设：<strong class="text-purple-600">认为在多线程环境中对共享资源的访问可能会发生冲突，因此需要通过锁机制来预防这种冲突</strong>。确保通过锁机制来防止多线程冲突。在实现同步时，线程会被阻塞直到获得锁，这种方式是基于对潜在冲突的防范。相对而言，乐观锁则基于对冲突发生概率较低的假设，采用不同的机制来处理并发访问。
            </p>
        </div>

        <!-- Concept Visualization -->
        <div class="content-card p-8 mb-12">
            <h2 class="text-2xl font-bold mb-6 text-gray-800">
                <i class="fas fa-sitemap mr-3 text-purple-600"></i>锁机制概念图
            </h2>
            <div class="mermaid">
                graph TD
                    A[多线程环境] --> B{synchronized 悲观锁}
                    B --> C[假设会发生冲突]
                    C --> D[独占访问]
                    C --> E[线程阻塞]
                    C --> F[自动锁管理]
                    
                    A --> G{乐观锁}
                    G --> H[假设冲突较少]
                    H --> I[CAS 操作]
                    H --> J[版本控制]
                    H --> K[失败重试]
                    
                    style B fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style G fill:#f093fb,stroke:#fff,stroke-width:3px,color:#fff
            </div>
        </div>

        <!-- Lock Mechanism Section -->
        <div class="grid md:grid-cols-2 gap-8 mb-12">
            <div class="feature-card">
                <h3 class="section-title text-xl font-bold mb-4 text-gray-800">
                    <i class="fas fa-cogs mr-2 text-purple-600"></i>锁的基本机制
                </h3>
                <ul class="space-y-3">
                    <li class="flex items-start">
                        <i class="fas fa-chevron-right text-purple-600 mt-1 mr-3"></i>
                        <div>
                            <strong class="text-gray-800">悲观假设：</strong>
                            <span class="text-gray-600">在多线程环境中，synchronized 假设多个线程可能同时尝试访问同一个资源，因此必须使用锁来防止数据竞争和状态不一致。</span>
                        </div>
                    </li>
                    <li class="flex items-start">
                        <i class="fas fa-chevron-right text-purple-600 mt-1 mr-3"></i>
                        <div>
                            <strong class="text-gray-800">独占访问：</strong>
                            <span class="text-gray-600">synchronized 确保在同一时刻只有一个线程可以执行被锁保护的代码块或方法，其他线程必须等待。</span>
                        </div>
                    </li>
                </ul>
            </div>

            <div class="feature-card">
                <h3 class="section-title text-xl font-bold mb-4 text-gray-800">
                    <i class="fas fa-key mr-2 text-purple-600"></i>锁的特性
                </h3>
                <ul class="space-y-3">
                    <li class="flex items-start">
                        <i class="fas fa-chevron-right text-purple-600 mt-1 mr-3"></i>
                        <div>
                            <strong class="text-gray-800">阻塞：</strong>
                            <span class="text-gray-600">如果一个线程试图获取已经被其他线程持有的锁，它会被阻塞，直到锁被释放。</span>
                        </div>
                    </li>
                    <li class="flex items-start">
                        <i class="fas fa-chevron-right text-purple-600 mt-1 mr-3"></i>
                        <div>
                            <strong class="text-gray-800">自动获取和释放：</strong>
                            <span class="text-gray-600">synchronized 自动处理锁的获取和释放，确保在进入临界区时持有锁，在退出临界区时释放锁。</span>
                        </div>
                    </li>
                </ul>
            </div>
        </div>

        <!-- Comparison Section -->
        <div class="highlight-box relative z-10 mb-12">
            <h3 class="text-2xl font-bold mb-4">
                <i class="fas fa-balance-scale mr-3"></i>悲观锁 vs 乐观锁
            </h3>
            <div class="grid md:grid-cols-2 gap-6">
                <div>
                    <h4 class="font-bold mb-2"><i class="fas fa-lock mr-2"></i>悲观锁（synchronized）</h4>
                    <p>基于"悲观"假设，认为冲突频繁发生，通过独占锁机制防止并发访问，确保数据一致性。</p>
                </div>
                <div>
                    <h4 class="font-bold mb-2"><i class="fas fa-unlock mr-2"></i>乐观锁（CAS）</h4>
                    <p>基于"乐观"假设，认为冲突较少发生，通过版本号或 CAS 操作检测冲突，失败时重试。</p>
                </div>
            </div>
        </div>

        <!-- Implementation Details -->
        <div class="content-card p-8 mb-12">
            <h3 class="section-title text-2xl font-bold mb-6 text-gray-800">
                <i class="fas fa-code mr-3 text-purple-600"></i>synchronized 的实现细节
            </h3>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-gray-50 p-6 rounded-lg">
                    <i class="fas fa-cube text-3xl text-purple-600 mb-3"></i>
                    <h4 class="font-bold mb-2 text-gray-800">同步块</h4>
                    <p class="text-gray-600">synchronized 可以用在方法上或代码块中，锁住方法或代码块中操作的对象。</p>
                </div>
                <div class="bg-gray-50 p-6 rounded-lg">
                    <i class="fas fa-lock text-3xl text-purple-600 mb-3"></i>
                    <h4 class="font-bold mb-2 text-gray-800">锁对象</h4